< previous page page_444 next page >

Page 444
In the example shown in Figure 22.2, the user must enter an employee ID, first name, last name, and salary for the employee to be added to the Employee object, which is then stored in a module-level collection. This module-level collection is called mcolEmps and is declared in the General Declarations area of the form:
9f31265bb40b592b1929e18a47ab28e5.gif
Dim mcolEmps As Collection
You need to declare a new instance of this collection in the Form_Load() event, as shown here:
9f31265bb40b592b1929e18a47ab28e5.gif
Private Sub Form_Load()
   txtEmpID = 1
   txtFirst = "Bill"
   txtLast = "Gates"
   txtSalary = "1000000"

   Set mcolEmps = New Collection
End Sub
You can now enter data in the employee form and click the Add command button to add that employee information to the module-level collection:
9f31265bb40b592b1929e18a47ab28e5.gif
Private Sub cmdAdd_Click()
   Dim oEmp As Employee

   Set oEmp = New Employee
   With oEmp
      .EmpID = CLng(txtEmpID)
      .FirstName = txtFirst
      .LastName = txtLast
      .Salary = CCur(txtSalary)
   End With

   ' Add New Object to Module Level Collection
   mcolEmps.Add oEmp, CStr(oEmp.EmpID)
End Sub
The first steps here are to create a new Employee object and then add the information from the text boxes to the appropriate properties of the Employee object. Next, the Add method of the Collection object is invoked and passed the Employee object, along with the string value of the employee ID. The employee ID must be converted to a string to create a Key, because the Key property only accepts a string value. Because all employees have unique employee IDs, this is an acceptable key for the collection.

 
< previous page page_444 next page >

If you like this book, buy it!